package com.uniledger.commonlib.pinkeyboard;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uniledger.commonlib.R;
import com.uniledger.commonlib.easymoney.EasyMoneyTextView;
import com.uniledger.commonlib.view.FullListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class KeyboardDialog extends BaseDialog implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private PasswordView mPasswordView;
    private KeyboardGridAdapter mKeyboardAdapter;

    private LinearLayout mPayLayout;
    private TextView mPayAmount;
    private TextView mLabelAmount;
    private TextView dialog_lack_balance;
    private ThirdPayInfo mPayInfo;

    private boolean isThird;

    private IKeyboardListener mOnListener;
    private IThirdPayListener mPayListener;

    public KeyboardDialog(Context context, boolean isThird) {
        this(context);
        this.isThird = isThird;
    }

    public KeyboardDialog(Context context) {
        this(context, R.style.KeyboardDialog);
    }

    public KeyboardDialog(Context context, int style) {
        super(context, style);
    }

    private void hidePayLayout() {
        mPayLayout.setVisibility(View.GONE);
    }

    private void showPayLayout() {
        mPayLayout.setVisibility(View.VISIBLE);
    }

    public void setThirdPayInfo(ThirdPayInfo thirdPayInfo) {

        this.mPayInfo = thirdPayInfo;

        this.mLabelAmount.setText(thirdPayInfo.payInfo.name);

        this.mPayAmount.setText(String.format(getContext().getResources().getString(R.string.keyboard_sub_tile),
                thirdPayInfo.payInfo.times,
                thirdPayInfo.payInfo.balance,
                thirdPayInfo.currency));
    }

    LinearLayout pinLayout;
    LinearLayout okLayout;

    TextView txt_desc_ok;
    EasyMoneyTextView txt_desc_info;
    FullListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.keyboard_dialog);
        setDialogSize(getContext().getResources().getDisplayMetrics().widthPixels, 0);
        setCanceledOnTouchOutside(false);

        pinLayout = (LinearLayout) findViewById(R.id.ll_pin);
        okLayout = (LinearLayout) findViewById(R.id.ll_ok);
        okLayout.setVisibility(View.GONE);

        findViewById(R.id.dialog_blank).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        findViewById(R.id.dialog_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        findViewById(R.id.dialog_forget_pwd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mOnListener) {
                    mOnListener.onForgetPwd();
                }
            }
        });

        mPayLayout = (LinearLayout) findViewById(R.id.ll_pay_info);
        dialog_lack_balance = (TextView) findViewById(R.id.dialog_lack_balance);
        dialog_lack_balance.setVisibility(View.GONE);
        mPayAmount = (TextView) findViewById(R.id.txt_amount);
        mLabelAmount = (TextView) findViewById(R.id.label_amount);
        if (isThird) {
            showPayLayout();
        } else {
            hidePayLayout();
        }
        mPasswordView = (PasswordView) findViewById(R.id.dialog_password);
        mPasswordView.setOnPasswordListener(new PasswordView.OnPasswordListener() {
            @Override
            public void onPasswordChange(String pwd) {
                if (null != mOnListener) {
                    mOnListener.onPasswordChange(pwd);
                }
            }

            @Override
            public void onPasswordComplete(String pwd) {
                if (null != mOnListener) {
                    mOnListener.onPasswordComplete(pwd);
                }

                if (null != mPayListener) {
                    mPayListener.onThirdComplete(pwd, mPayInfo);
                }
            }

        });

        GridView keyboardGrid = (GridView) findViewById(R.id.dialog_grid);

        List<Integer> datas = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            datas.add(i);
        }
        List<Integer> randomList = randomList(datas);
        randomList.add(9, KeyboardGridAdapter.TYPE_VALUE_BANK);
        randomList.add(KeyboardGridAdapter.TYPE_VALUE_DELETE);
        mKeyboardAdapter = new KeyboardGridAdapter(randomList);
        keyboardGrid.setAdapter(mKeyboardAdapter);
        keyboardGrid.setOnItemClickListener(this);
        keyboardGrid.setOnItemLongClickListener(this);

        txt_desc_ok = (TextView) findViewById(R.id.txt_desc_ok);
        txt_desc_info = (EasyMoneyTextView) findViewById(R.id.txt_desc_info);
        listView = (FullListView) findViewById(R.id.pay_list);

    }

    /**
     * 回调数据
     *
     * @param thirdResult
     */
    public void showOkData(PayThirdResult thirdResult) {
        pinLayout.setVisibility(View.GONE);
        okLayout.setVisibility(View.VISIBLE);
        txt_desc_ok.setText(thirdResult.flagStr + " Successful");
        txt_desc_info.setText(thirdResult.amount);
        txt_desc_info.setCurrencySymbol(thirdResult.currencySymbol, R.drawable.ic_common_black_aed);
        List<PayThirdDesc> list = PayThirdDesc.payDescList(thirdResult);
        PayThirdCompleteAdapter adapter = new PayThirdCompleteAdapter(list, getContext());
        listView.setAdapter(adapter);
    }

    public void setKeyboardListener(IKeyboardListener listener) {
        this.mOnListener = listener;
    }

    public void setPayListener(IThirdPayListener payListener) {
        this.mPayListener = payListener;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (null == mKeyboardAdapter) {
            return;
        }
        if (position == (mKeyboardAdapter.getCount() - 3)) {
            return;
        }
        if (position == (mKeyboardAdapter.getCount() - 1)) {
            mPasswordView.deletePassword();
        } else {
            int value = mKeyboardAdapter.getItem(position);
            mPasswordView.addPassword(value);
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == mKeyboardAdapter.getCount() - 1) {
            mPasswordView.clearPassword();
            return true;
        }
        return false;
    }

    private static <V> List<V> randomList(List<V> sourceList) {
        if (isEmpty(sourceList)) {
            return sourceList;
        }

        List<V> randomList = new ArrayList<V>(sourceList.size());
        do {
            int randomIndex = Math.abs(new Random().nextInt(sourceList.size()));
            randomList.add(sourceList.remove(randomIndex));
        } while (sourceList.size() > 0);

        return randomList;
    }

    private static <V> boolean isEmpty(List<V> sourceList) {
        return (sourceList == null || sourceList.size() == 0);
    }

    public void clearPassword() {
        mPasswordView.clearPassword();
    }

    public void showError(int flag) {
        dialog_lack_balance.setVisibility(flag);
    }

    @Override
    public void show() {
        super.show();
        pinLayout.setVisibility(View.VISIBLE);
        okLayout.setVisibility(View.GONE);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        clearPassword();
    }
}
